﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace AncientEmpires_025_132
{
    public class Sprite2D
    {
        private Vector2 _position;
        public Vector2 Position
        {
            get { return _position; }
            set { _position = value; }
        }

        private Texture2D[] _textures;
        public Texture2D[] Textures
        {
            get { return _textures; }
            set
            {
                _textures = value;
                _nTextures = _textures.Length;
                _iTexture = 0;
            }
        }

        private int _nTextures;
        public int NTextures
        {
            get { return _nTextures; }
            set { _nTextures = value; }
        }

        private int _iTexture;
        public int ITexture
        {
            get { return _iTexture; }
            set { _iTexture = value; }
        }
        private Vector2 _size;
        public Vector2 Size
        {
            get { return _size; }
            set { _size = value; }
        }

        private Vector2 _topLeft;
        public Vector2 TopLeft
        {
            get { return _topLeft; }
            set { _topLeft = value; }
        }

        private float _normalDelay;

        public float NormalDelay
        {
            get { return _normalDelay; }
            set { _normalDelay = value; }
        }
        public Sprite2D(Texture2D[] texture, Vector2 position)
        {
            _normalDelay = 16.0f;
            Textures = texture;
            _position = position;
            TopLeft = new Vector2();
            _nTextures = texture.Length;
        }

        public Sprite2D(Texture2D[] texture, Vector2 position, Vector2 topleft)
        {
            _normalDelay = 16.0f;
            _textures = texture;
            _position = position;
            _topLeft = topleft;
            _nTextures = texture.Length;
        }

        public void ChangePosition(int cordinate, int num)
        {
            if (cordinate == 0)
            {
                _position.X += num;
            }
            else _position.Y += num;
        }

        public void SetPosition(Vector2 setPos)
        {
            Position = setPos;
        }

        public bool CheckClick(Vector2 ms)
        {
            if (ms.X >= this.Position.X
               && ms.X <= this.Position.X + this.Size.X
               && ms.Y >= this.Position.Y
               && ms.Y <= this.Position.Y + this.Size.Y)
            {
                return true;
            }
            return false;
        }

        public void Update(GameTime gameTime)
        {
            _iTexture = (_iTexture + 1) % _nTextures;
        }

        public void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            //spriteBatch.Draw(_textures[_iTexture], _position, Color.White);

            Rectangle rect = new Rectangle();
            if (Size.X == 0 && Size.Y == 0)
            {
                Size = new Vector2(Textures[ITexture].Width, Textures[ITexture].Height);
            }
            rect = new Rectangle((int)Position.X, (int)Position.Y, (int)Size.X, (int)Size.Y);
            spriteBatch.Draw(Textures[ITexture], rect, Color.White);
        }
    }
}
